Skip to content

Add type hints and update docstrings for compact test suite modules - #1266

Merged
bact merged 7 commits into
devfrom
copilot/add-type-hints-to-submodules-one-more-time
Feb 3, 2026
Merged

Add type hints and update docstrings for compact test suite modules#1266
bact merged 7 commits into
devfrom
copilot/add-type-hints-to-submodules-one-more-time

Conversation

Copilot AI commented Feb 3, 2026

Copy link
Copy Markdown
Contributor

What do these changes do

Adds comprehensive type hints to functions tested in the compact test suite, achieving 100% coverage for:

  • pythainlp.util.spell_word()
  • pythainlp.util.thai_word_tone_detector()
  • pythainlp.transliterate.pyicu.transliterate()
  • pythainlp.classify.GzipModel.predict()

Updates docstrings to accurately reflect the new type hints, including parameter types, return types, and edge case behavior.

What was wrong

Functions lacked type hints or had mypy errors preventing type checking:

  • spell_word() and thai_word_tone_detector() accepted str but tests passed None
  • External library calls in pyicu and numpy operations returned Any, causing mypy errors
  • Docstrings were outdated with incorrect parameter names, wrong types, and missing documentation for Optional parameters

How this fixes it

Type hints for None handling:

# Before
def spell_word(text: str) -> list[str]:

# After  
def spell_word(text: Optional[str]) -> list[str]:
    if not text:
        return []

Fixed mypy no-any-return errors:

  • Wrapped pyicu.Transliterator.transliterate() with str() cast
  • Wrapped numpy.ndarray indexing in GzipModel.predict() with str() cast

Updated docstrings:

  • Fixed parameter name mismatches (e.g., wtext)
  • Corrected parameter types to include Optional[str] where applicable
  • Updated return types to use modern syntax (list[str] instead of List[str])
  • Fixed incorrect return types (e.g., Tuple[str, str]list[tuple[str, str]])
  • Added examples demonstrating None/empty input behavior
  • Fixed typos and improved clarity

Uses Optional[T] instead of T | None for Python 3.9 compatibility.

Your checklist for this pull request

  • Passed code styles and structures
  • Passed code linting checks and unit test
Original prompt

Iterating to incrementally add type hints to submodules,
based on works done in these PRs:
#1262
#1263
#1264
#1265

Strategy

  • Start with small functions/classes and functions/classes that require few external dependencies.
  • Moving gradually one function by one function, in the four test suites: starting from "core", then "compact", then "extra", and "noauto".

Goals

  • Immediate goal for this session - 100% type hints for functions/classes being called in "compact" test suite.
  • Ultimate goal (outside of this session) is to make the package a typed package, while keeping maintainability.

Instructions

  • Follow best practices and standard Python type hint patterns.
  • Start small in the area with high confidence (like highly tested submodules or functions), then gradually grow one submodule at a time.
  • Use mypy as main assistant.
    • mypy is already in "dev" extra dependencies in pyproject.toml
    • mypy configuration is in pyproject.toml
    • Sometimes mypy may report errors wrongly due to cache issues. Try to reset the cache.
  • Use pyright, pyrefly, and pytype for second opinions.
  • Required dependencies for each test suite are in pyproject.toml. Install them to avoid errors. See https://github.com/PyThaiNLP/pythainlp/blob/dev/tests/README.md
  • Use native type in type hint (list, tuple, .. instead of List, Tuple, ..)
  • Do not use A | B style Union, use full Union[] instead.
  • We want to keep compatibility with Python 3.9 strict and allow type inspection tools to work correctly.
  • When insert typing imports, put it in appropriate location and order
  • Use "if TYPE_CHECKING import" block when possible.
  • When reordering the imports, beware of circular import. Read comments near imports to get more information.
  • Remove unused imports
  • Do not allow the use of assert in production code (it is only allowed for testing and debugging).
  • Try to fix the type, the scope logic, or do the casting, before putting #type: ignore comment.
  • When dealing with types from external libraries:
    • Check if type information is provided (maybe through stubs). If it is, cast wrapper may not necessary.
    • If return type is not provided, may be it is appropriate to use # type: ignore[no-any-return] annotation for clarity.
  • Recheck necessity when do casting.
  • Recheck necessity of # noqa: and # type: ignore
  • Provide stubs as necessary.
  • After changes for type hints, recheck for functionality correctness.
  • Recheck code, docstring, documentation consistency.
  • Recheck formatting with ruff
  • Do another code review
  • Address code review feedback

💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI assigned Copilot and bact Feb 3, 2026
@bact bact added the refactoring a technical improvement which does not add any new features or change existing features. label Feb 3, 2026
@bact bact added this to the 5.3 milestone Feb 3, 2026
Copilot AI and others added 2 commits February 3, 2026 10:04
Co-authored-by: bact <128572+bact@users.noreply.github.com>
…ve Optional usage

Co-authored-by: bact <128572+bact@users.noreply.github.com>
@bact
bact marked this pull request as ready for review February 3, 2026 10:09
Copilot AI changed the title [WIP] Add type hints to submodules incrementally Add type hints to compact test suite modules Feb 3, 2026
@coveralls

coveralls commented Feb 3, 2026

Copy link
Copy Markdown

Coverage Status

coverage: 65.762% (+0.001%) from 65.761%
when pulling 77e4b8c on copilot/add-type-hints-to-submodules-one-more-time
into 0c21974 on dev.

Comment thread pythainlp/util/thai.py Outdated
@bact

bact commented Feb 3, 2026

Copy link
Copy Markdown
Member

@copilot Recheck the docstring and documentation of modified functions/modules. See if they are up to date with the latest type hints.

Co-authored-by: bact <128572+bact@users.noreply.github.com>

Copilot AI commented Feb 3, 2026

Copy link
Copy Markdown
Contributor Author

@copilot Recheck the docstring and documentation of modified functions/modules. See if they are up to date with the latest type hints.

Updated all docstrings to match the type hints in commit 55f52e9:

  • Fixed parameter names and types (e.g., wtext, strOptional[str])
  • Corrected return type documentation (List[str]list[str], Tuple[str, str]list[tuple[str, str]])
  • Fixed parameter type for k in GzipModel.predict() from str to int
  • Fixed typo "Internaitonal" → "International" in pyicu
  • Added examples showing None/empty input behavior

All 19 compact tests pass.

Copilot AI changed the title Add type hints to compact test suite modules Add type hints and update docstrings for compact test suite modules Feb 3, 2026
Copilot AI requested a review from bact February 3, 2026 10:20
bact added 2 commits February 3, 2026 10:22
Update the return value for None or empty word in thai_word_tone_detector function.
@sonarqubecloud

sonarqubecloud Bot commented Feb 3, 2026

Copy link
Copy Markdown

@bact
bact merged commit a22ff19 into dev Feb 3, 2026
21 of 32 checks passed
@bact
bact deleted the copilot/add-type-hints-to-submodules-one-more-time branch February 3, 2026 10:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

refactoring a technical improvement which does not add any new features or change existing features.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants